<template>
  <div class="fast-reply">
    <!-- nav -->
    <el-radio-group v-model="currentTab" class="reply-nav" fill="#66B1FF">
      <el-radio-button label="common">公共术语</el-radio-button>
      <el-radio-button label="my">我的术语</el-radio-button>
    </el-radio-group>

    <!-- 快捷语 -->
    <div class="common-replys scroll">
      <div class="common-groups" v-for="(cps, index) in groups" :key="index">
        <!-- 分组 -->
        <div class="common-groups-tname"  @click="toggleCommonList(index)">
          <span class="ellipsis" v-text="index"></span>
          <i
            v-if="currentTab === 'my'"
            class="fa fa-plus"
            aria-hidden="true"
            @click="addReply(index)"
          ></i>
          <i
            class="fa fa-chevron-circle-down"
            title="显示/收起"
            aria-hidden="true"
          ></i>
        </div>
        <!-- 组内消息 -->
        <div v-show="showGroups.indexOf(index)===-1">
          <div class="every-reply" v-for="(cp, index) in cps" :key="index" @click="sendMsg(cp)">
            <div class="cp-name">
              <span class="cpname ellipsis" v-text="cp.name"></span>
              <i
                class="fa fa-minus remove"
                v-if="currentTab === 'my'"
                aria-hidden="true"
                @click.stop="removeReply(cp)"
              ></i>
              <div class="show-cp" @click.stop="showCp(cp)">
                <i
                  v-if="currentId !== cp.id && cp.ctype !== 5"
                  class="fa fa-eye-slash"
                  aria-hidden="true"
                ></i>
                <i
                  v-if="currentId === cp.id && cp.ctype !== 5"
                  class="fa fa-eye"
                  aria-hidden="true"
                ></i>
              </div>
            </div>
            <div class="reply-detail" v-show="cp.id === currentId || cp._id === currentId">
              <p v-if="cp.ctype===1" v-text="cp.content"></p>
              <img v-else-if="cp.ctype === 2" :src="cp.content" alt="图片" />
              <video v-else-if="cp.ctype === 3" :src="cp.content" controls></video>
              <audio v-else-if="cp.ctype === 4" :src="cp.content" controls></audio>
            </div>
          </div>
        </div>
      </div>

      <el-button
        v-if="currentTab === 'my' && myReply.length === 0"
        class="add-my-reply"
        type="primary"
        size="small"
        @click="addReplyVisible=true"
      >添加</el-button>
    </div>

    <!-- 添加常用语的弹框 -->
    <el-dialog title="添加常用语" :visible.sync="addReplyVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="组名" prop="tname">
          <el-input v-model="ruleForm.tname" maxlength="20" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="name">
          <el-input v-model="ruleForm.name" maxlength="20" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 4}"
            v-model="ruleForm.content"
            maxlength="500"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  name: 'FastReply',
  data() {
    return {
      currentTab: 'common', // 0是publicReply 1是myReply
      currentId: -1, // 显示某个reply
      showGroups: [], // 要显示的分组
      addReplyVisible: false, // 显示dialog
      ruleForm: {
        ctype: 1, // 常用语类型 1.文本 2.图片 3.时评 4.语音 5.文件
        tname: '', // 组名
        name: '', // 备注
        content: '', // 常用语
      },
      rules: {
        tname: [
          { required: true, message: '请输入组名', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' },
          { min: 1, max: 500, message: '长度在 1 到 500 个字符', trigger: 'blur' }
        ],
      }
    }
  },

  computed: {
    ...mapGetters({
      publicReply: 'conversation/publicReply',
      myReply: 'nedb/myReply'
    }),
    // reply分组
    groups() {
      let currentGroups = {}
      let currentReply = this.currentTab === 'common' ? this.publicReply : this.myReply
      if (this.publicReply.length > 0) {
        for (const cr of currentReply) {
          if (currentGroups[cr.tname]) {
            currentGroups[cr.tname].push(cr)
          } else {
            let crs = []
            crs.push(cr)
            currentGroups[cr.tname] = crs
          }
        }
      }
      return currentGroups
    }
  },
  methods: {
    // 隐藏/显示分组
    toggleCommonList(index) {
      let id = this.showGroups.indexOf(index)
      if (id === -1) {
        this.showGroups.push(index)
      } else {
        this.showGroups.splice(id, 1)
      }
    },
    // 显示消息内容
    showCp(cp) {
      let cid = cp.id || cp._id
      if (this.currentId === cid) {
        this.currentId = -1
      } else {
        this.currentId = cp.id ? cp.id : cp._id
      }
    },
    // 发送常用消息
    sendMsg(msg) {
      let type = 'Text'
      switch (msg.ctype) {
        case 1:
          type = 'Text'
          break
        case 2:
          type = 'Picture'
          break
        case 3:
          type = 'Video'
          break
        case 4:
          type = 'Voice'
          break
        case 5:
          type = 'File'
          break
        default:
          type = 'Text'
          break
      }
      let content = msg.content
      if (type === 'File') {
        let fileName = msg.name + msg.content.replace(/.*\./, '.')
        content =
          JSON.stringify({ name: fileName, url: msg.content, size: '', Source: '公共术语' })
      }
      this.$store.commit('conversation/SET_CONTENT_TYPE', type)
      this.$store.commit('conversation/SET_CONTENT', content)
    },
    // 添加常用语
    addReply(info) {
      this.ruleForm.tname = info
      this.addReplyVisible = true
    },
    // 删除我的常用语
    removeReply(reply) {
      this.$store.dispatch('nedb/RemoveMyReply', reply._id)
    },
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let doc = {
            ctype: this.ruleForm.ctype,
            tname: this.ruleForm.tname,
            name: this.ruleForm.name,
            content: this.ruleForm.content,
            createTime: new Date().getTime(),
          }
          this.$store.dispatch('nedb/AddMyReply', doc)
          this.addReplyVisible = false
          this.resetForm('ruleForm')
        } else {
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    // 如果vuex没有publicReply 就从服务器拉取
    if (this.publicReply.length === 0) {
      this.$store.dispatch('websocket/GetCommonTerms')
    }
    if (this.myReply.length === 0) {
      this.$store.dispatch('nedb/SetMyReply')
    }
  }
}
</script>

<style lang="scss" scoped>
.fast-reply {
  width: 100%;
  height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;

  .reply-nav {
    width: 100%;
    margin: 5px;
    .el-radio-button {
      width: 50%;
      ::v-deep .el-radio-button__inner {
        width: 100%;
        height: 30px;
        line-height: 30px;
        padding: 0;
      }
    }
  }

  .common-replys {
    width: 100%;
    height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
    position: relative;

    .common-groups {
      margin-bottom: 5px;
      .common-groups-tname {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background: #b3d8ff;
        padding: 5px 0;
        cursor: pointer;
        span,
        i {
          color: white;
        }
        span {
          width: 70%;
          padding-left: 5px;
          font-size: 16px;
          display: inline-block;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        i {
          width: 10%;
          text-align: center;
          font-size: 16px;
          cursor: pointer;
          &:hover {
            color: #4bbcfb;
          }
        }
      }
      .every-reply {
        margin-top: 5px;
        margin-bottom: 5px;
        width: 100%;
        padding: 5px 0;
        background: #f5f5f5;
        &:hover {
          background: #ffffff;
        }
        .cp-name {
          display: flex;
          justify-content: space-between;
          align-items: center;
          .cpname {
            margin-left: 5px;
            font-size: 14px;
            width: 70%;
          }
          .remove,
          .show-cp {
            width: 10%;
            font-size: 14px;
            text-align: center;
            cursor: pointer;
          }
        }
        .reply-detail {
          padding: 5px;
        }

        p {
          margin-left: 3%;
          width: 94%;
          font-size: 12px;
          line-height: 20px;
          word-break: break-all;
        }
        img,
        audio,
        video {
          width: auto;
          height: auto;
          max-width: 80%;
          min-width: 80%;
          min-height: 40px;
          margin-left: 10%;
        }
      }
    }
    .add-my-reply {
      width: 200px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }

  .selected {
    color: #3b7957;
  }

  //   ::v-deep .el-dialog {
  //     // width: 600px;
  //     // height: 400px;
  //     border: solid red 1px;
  //   }
}
</style>

